#include <pthread.h>
#include <time.h>
#include <iostream>
#include <stdlib.h>
#include <cstring>
int main()
{
    int err;
    struct timespec tout;
    struct tm *tmp;
    char buf[64];
    pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_lock(&lock);

    std::cout << "mutex is lock" << std::endl;
    clock_gettime(CLOCK_REALTIME, &tout);
    tmp = localtime(&tout.tv_sec);
    strftime(buf, sizeof(buf), "%r", tmp);
    std::cout << "current time is " << buf << std::endl;

    tout.tv_sec += 10; // 10秒以后
    err = pthread_mutex_timedlock(&lock, &tout);
    clock_gettime(CLOCK_REALTIME, &tout);
    tmp = localtime(&tout.tv_sec);
    strftime(buf, sizeof(buf), "%r", tmp);
    std::cout << "current time is " << buf << std::endl;

    if(err == 0)
        std::cout<<"mutex locked again!"<<std::cout;
    else
        std::cout<<"can't lock mutex again :"<<strerror(err);
    return 0;
}